package com.dongjiantong.app.controller.farmrecords.cowRecords;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.exceptions.OPServerException;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.common.web.sso.LoginInfo;
import com.dongjiantong.common.web.util.UserUtil;
import com.dongjiantong.farmrecords.dto.FarmRecordsImmunizationDto;
import com.dongjiantong.farmrecords.model.FarmRecordsImmunization;
import com.dongjiantong.farmrecords.service.FarmRecordsImmunizationService;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 牛档案-免疫记录表
 */

@Slf4j
@OpenBean
public class FarmRecordsBullImmunizationController {

    @Reference(version = "1.0.0")
    public FarmRecordsImmunizationService farmRecordsImmunizationService;

    /**
     * 根据关联牛档案id 查看免疫记录
     * @param cowRecordId
     * @return
     */
    @OpenMethod(key = "app.farmrcords.farmrecordsimmunization.bull.selectBullImmunization",
            paramNames = "cowRecordId",
            httpMethods = RequestMethod.GET)
    public Response<Map<String,Object>> selectBullImmunization(Long cowRecordId) {
        if (cowRecordId == null) { throw new OPServerException("关联牛档案id不能为空"); }

        Map<String,Object> result = Maps.newHashMap();

        FarmRecordsImmunization farmRecordsImmunization = farmRecordsImmunizationService.selectBullImmunization(cowRecordId);

        if (farmRecordsImmunization == null) {
            result.put("data",null);
        }
        result.put("data",farmRecordsImmunization);
        return Response.ok(result);
    }

    /**
     * 新增一条牛档案免疫记录
     * @param farmRecordsImmunizationDto
     * @param toStatus
     * @return long 返回插入免疫记录的id
     */
    @OpenMethod(key = "app.farmrecords.farmrecordsimmunization.bull.createOne",
            paramNames = {"farmRecordsImmunizationDto","toStatus"},
            httpMethods = RequestMethod.POST)
    public Response<Map<String,Object>> createOne(@RequestBody(required = false) FarmRecordsImmunizationDto farmRecordsImmunizationDto,
                                                  @RequestParam(required = false) Integer toStatus){
        if (farmRecordsImmunizationDto == null) {
            throw new OPServerException(HttpCode.BAD_REQUEST, "请求参数对象不能为空");
        }
        if (farmRecordsImmunizationService == null){
            throw new OPServerException("farmRecordsImmunizationService 为null,请检查");
        }

        LoginInfo loginInfo = UserUtil.getCurrentUser();

        Map<String,Object> result = farmRecordsImmunizationService.createCowOne(loginInfo,farmRecordsImmunizationDto,toStatus);

        return Response.ok(result);
    }

    /**
     * 更新一条单据信息
     * @param farmRecordsImmunizationDto
     * @return  boolean (更新成功返回true 反之返回false)
     */
    @OpenMethod(key = "app.farmrecords.farmrecordsimmunization.bull.updateByIdOrNumber",
            paramNames = {"farmRecordsImmunizationDto","toStatus"},
            httpMethods = RequestMethod.POST)
    public Response<Map<String,Object>> updateByIdOrNumber(@RequestBody (required = false)FarmRecordsImmunizationDto farmRecordsImmunizationDto,
                                                @RequestParam(required = false) Integer toStatus){
        if (farmRecordsImmunizationService == null){
            throw new OPServerException("farmRecordsImmunizationService 为null,请检查");
        }

        LoginInfo loginInfo = UserUtil.getCurrentUser();

        Map<String,Object> map = farmRecordsImmunizationService.updateCowByIdOrNumber(loginInfo,farmRecordsImmunizationDto,toStatus);
        return Response.ok(map);
    }
}
